<?php
defined( "AZ_EXE_" ) or die ( "CORE VARIABLE IS NOT DEFINED." );
	class TrackVisitorModel extends Model {
		function trackVisitor(){
			$timezone = "Asia/Bangkok";
			if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
			if(!isset($_SESSION['track_visitor'])){				
				$query = "
					INSERT INTO `track_visitor` SET `ip_address` = '" . $_SERVER['REMOTE_ADDR'] . "', `user_agent` = '" . $_SERVER['HTTP_USER_AGENT'] . "', `created` = '" . Date('Y-m-d h:i:s') . "'
					";	
				$this->executeQuery($query);
				$_SESSION['track_visitor'] = 1;
			}else{
				$query = "
						SELECT `id` FROM `track_visitor` WHERE `ip_address` = '" . $_SERVER['REMOTE_ADDR'] . "' AND `user_agent` = '" . $_SERVER['HTTP_USER_AGENT'] . "' AND DATE(`created`) = DATE('" . Date('Y-m-d h:i:s') . "')
						";
				$result = $this->executeQueryAsObject($query);
				$num = count($result);
				if($num == 0){
					$query = "
					INSERT INTO `track_visitor` SET `ip_address` = '" . $_SERVER['REMOTE_ADDR'] . "', `user_agent` = '" . $_SERVER['HTTP_USER_AGENT'] . "', `created` = '" . Date('Y-m-d h:i:s') . "'
					";
					$this->executeQuery($query);
				}
			}
		}
		function getVisitor(){
			$timezone = "Asia/Bangkok";
			if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
			$query = "
					SELECT COUNT(`id`) as visitor_today, 
					(SELECT COUNT(`id`) FROM `track_visitor` WHERE WEEKOFYEAR(`created`)=WEEKOFYEAR('" . Date('Y-m-d h:i:s') . "')) as visitor_this_week,
					(SELECT COUNT(`id`) FROM `track_visitor` WHERE WEEKOFYEAR(`created`)=WEEKOFYEAR('" . Date('Y-m-d h:i:s') . "')-1) as visitor_last_week,
					(SELECT COUNT(`id`) FROM `track_visitor`) as total_visitor
					FROM `track_visitor` WHERE DATE(`created`) = DATE('" . Date('Y-m-d h:i:s') . "')
					";
			$result = $this->executeQueryAsObject($query);
			return $result[0];
		}
	}
?>